package com.baiduspring.utils.mybatis;//package com.com.baiduspring.utils.mybatis;
//
//import lombok.extern.slf4j.Slf4j;
//import org.apache.ibatis.io.DefaultVFS;
//import org.apache.ibatis.io.VFS;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.mybatis.spring.SqlSessionFactoryBean;
//import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
//import org.springframework.core.io.support.ResourcePatternResolver;
//import org.springframework.util.ResourceUtils;
//
//import java.io.IOException;
//import java.net.MalformedURLException;
//import java.net.URL;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.jar.JarEntry;
//import java.util.jar.JarInputStream;
////import net.sourceforge.stripes.vfs.DefaultVFS;
//
//@Slf4j
//@Configuration
//public class SpringBootVfs extends DefaultVFS {
//    @Override
//    protected URL findJarForResource(URL url) throws MalformedURLException {
//        url = ResourceUtils.extractJarFileURL(url);
//        return (isJar(url)) ? url : null;
//    }
//
//    @Override
//    protected boolean isJar(URL url) {
//        return url.getPath().toLowerCase().endsWith(ResourceUtils.JAR_FILE_EXTENSION)
//                || url.getPath().toLowerCase().endsWith(".war");
//    }
//
//    /**
//     * List the names of the entries in the given {@link JarInputStream} that
//     * begin with the specified {@code path}. Entries will match with or without
//     * a leading slash.
//     *
//     * @param jar  The JAR input stream
//     * @param path The leading path to match
//     * @return The names of all the matching entries
//     * @throws IOException If I/O errors occur
//     */
//    @Override
//    protected List listResources(JarInputStream jar, String path) throws IOException {
//// Include the leading and trailing slash when matching names
//        if (!path.startsWith("/"))
//            path = "/" + path;
//        if (!path.endsWith("/"))
//            path = path + "/";
//// Iterate over the entries and collect those that begin with the
//// requested path
//        List resources = new ArrayList();
//        for (JarEntry entry; (entry = jar.getNextJarEntry()) != null; ) {
//            if (!entry.isDirectory()) {
//// Add leading slash if it's missing
//                String name = entry.getName();
//                if (!name.startsWith("/"))
//                    name = "/" + name;
//// Check file name
//                if (name.startsWith(path) || name.startsWith("/BOOT-INF/classes" + path)) {
//                    log.trace("Found resource: ", name);
//                    resources.add(name.substring(1)); // Trim leading slash
//                }
//            }
//        }
//        return resources;
//    }
//
//    @Bean(name = "sqlSessionFactory")
//    public SqlSessionFactory sqlSessionFactoryBean() {
//    VFS.addImplClass(SpringBootVFS.class);
////解决myBatis下 不能嵌套jar文件的问题
//        VFS.addImplClass(SpringBootVFS.class);
//        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
////        bean.setDataSource(dataSource);
////        bean.setTypeAliasesPackage("org.weichai");
//// 分页插件
////添加XML目录
//        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//        try {
//// 添加插件
//            bean.setMapperLocations(resolver.getResources("classpath*:mapper/**/*.xml"));
//            return bean.getObject();
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new RuntimeException(e);
//        }
//    }
//}
